package com.bite.ojsystem.controller;

import com.bite.ojcommoncore.constants.HttpConstants;
import com.bite.ojcommoncore.domain.result.R;
import com.bite.ojcommoncore.enums.ResultCode;
import com.bite.ojsystem.domain.SysUser;
import com.bite.ojsystem.domain.dto.LoginSysUserDto;
import com.bite.ojsystem.domain.vo.SysUserVO;
import com.bite.ojsystem.service.SysUserService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "系统管理员接口")
@RestController
@RequestMapping("/sysUser")
@Slf4j
public class SysUserController {


    @Autowired
    private SysUserService sysUserService;

    @PostMapping("/login")
    public <T> R<T> login(@RequestBody LoginSysUserDto loginSysUserDto) {
        log.info("系统管理员登录");
        // TODO 用户登录成功需要保存用户登录信息 -- 比如token
        return sysUserService.login(loginSysUserDto);
    }


    @PostMapping("/save")
    public <T> R<T> save(@RequestBody LoginSysUserDto loginSysUserDto) {
        log.info("系统管理员信息保存");
        // 用户保存

        if (sysUserService.save(loginSysUserDto)) {
            return R.ok();
        }
        return R.fail(ResultCode.FAILED.getCode() , ResultCode.FAILED.getMsg());
    }


    @GetMapping("/sysUserInfo")
    public R<SysUserVO> sysUserInfo(
            @NonNull @RequestHeader(HttpConstants.AUTHENTICATION)String token ) {
        log.info("获取登录用户信息");
        SysUserVO sysUserVO = sysUserService.getCurrentUser(token);
        return R.ok(sysUserVO);
    }
}
